import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/widgets/tabbar/gt_thirdly_tabbar_v3.dart';

/// @Title   Card类型的tab
/// @Author: bryce
/// @CreateDate: 2023年10月11日20:38:42
enum GTCardTabBarSizeV3 { height32, height40 }

class GTCardTabBar extends StatelessWidget {
  final List<String>? tabTitles;
  final List<GTTabModalV3>? tabData;
  final TabController? controller;

  // 是否占据父组件总宽度，若为true时，tab平分总宽度；
  final bool? isMatchParentWidth;

  // 卡片宽度,若传了cardWidth则卡片为固定宽度，否则卡片宽度为自适应文字宽度
  final double? cardWidth;

  // 新增tab切换拦截，如果返回true代表拦截，false允许切换
  final Future<bool> Function(TabController mController, int newIndex)? onInterceptSwitch;

  // 新增拦截点击tab时的切换（对于使用controller控制的和手势滚动TabBarView的切换不拦截）
  final Future<bool> Function(TabController mController, int newIndex)? onInterceptTapSwitch;
  final GTCardTabBarSizeV3? size;
  final Color? selectedLabelColor;
  final Color? selectedTextColor;
  final ValueChanged<int>? onTap;

  const GTCardTabBar({
    super.key,
    this.tabTitles,
    this.tabData,
    this.controller,
    this.cardWidth,
    this.isMatchParentWidth,
    this.onInterceptSwitch,
    this.onInterceptTapSwitch,
    this.size = GTCardTabBarSizeV3.height32,
    this.selectedLabelColor,
    this.selectedTextColor,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    double height = 32;
    double tabRadius = 16, indicatorRadius = 14;
    if ((size ?? GTCardTabBarSizeV3.height32) == GTCardTabBarSizeV3.height40) {
      height = 40;
      tabRadius = 70;
      indicatorRadius = 70;
    }

    return Container(
      padding: const EdgeInsets.all(2),
      height: height,
      alignment: (isMatchParentWidth ?? false) ? Alignment.center : null,
      decoration:
          BoxDecoration(color: GTColorsV3.uikitNeutral3V3.color, borderRadius: BorderRadius.circular(tabRadius)),
      child: GTTabBar(
        dividerColor: Colors.transparent,
        controller: controller,
        isScrollable: (isMatchParentWidth ?? false) ? false : true,
        unselectedLabelColor: GTColorsV3.uikitText3V3.color,
        labelColor: selectedTextColor ?? GTColorsV3.uikitText1V3.color,
        labelStyle: gtTextStyleW500(fontSize: 14),
        unselectedLabelStyle: gtTextStyleW500(fontSize: 14),
        indicatorSize: TabBarIndicatorSize.label,
        labelPadding: EdgeInsets.zero,
        onInterceptSwitch: onInterceptSwitch,
        onInterceptTapSwitch: onInterceptTapSwitch,
        indicator: BoxDecoration(
            color: selectedLabelColor ?? GTColorsV3.cmpt1.color, borderRadius: BorderRadius.circular(indicatorRadius)),
        tabs: tabs(),
        onTap: (index) {
          onTap?.call(index);
        },
      ),
    );
  }

  List<Widget> tabs() {
    if (tabTitles != null && tabTitles?.isNotEmpty == true) {
      return tabTitles!.map((e) => _tabItem(e)).toList();
    } else if (tabData != null && tabData?.isNotEmpty == true) {
      return tabData!.map((e) => _tabDataItem(e)).toList();
    } else {
      return [];
    }
  }

  Widget _tabItem(String title) {
    return Tab(
      child: Container(
        padding: (isMatchParentWidth ?? false) || cardWidth != null ? null : const EdgeInsets.symmetric(horizontal: 16),
        height: (size ?? GTCardTabBarSizeV3.height32) == GTCardTabBarSizeV3.height40 ? 32 : 40,
        width: cardWidth,
        alignment: Alignment.center,
        child: Text(title),
      ),
    );
  }

  Widget _tabDataItem(GTTabModalV3 bean) {
    return Tab(
      child: Container(
        padding: (isMatchParentWidth ?? false) || cardWidth != null ? null : const EdgeInsets.symmetric(horizontal: 16),
        height: (size ?? GTCardTabBarSizeV3.height32) == GTCardTabBarSizeV3.height40 ? 32 : 40,
        width: cardWidth,
        alignment: Alignment.center,
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            if (bean.leftIcon != null) ...[
              bean.leftIcon!,
              SizedBox(width: bean.iconPadding),
            ],
            Text(bean.text),
            if (bean.rightIcon != null) ...[
              SizedBox(width: bean.iconPadding),
              bean.rightIcon!,
            ],
          ],
        ),
      ),
    );
  }
}
